import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginAPI,registerAPI } from '@/apis/user'
import { useCartStore } from './cartStore'

export const useUserStore = defineStore('user', () => {

  const userInfo = ref({})
  const cartStore = useCartStore()

  const getUserInfo = async ({ account, password }) => {
    const res = await loginAPI({ account, password })
    userInfo.value = res.data.data
  }
  const registerUser = async ({ account, nickname, password }) => {
    await registerAPI({ account, nickname,password })
  }
  const clearUserInfo = () => {
    userInfo.value = {}
    cartStore.clearCart
  }

  return {
    userInfo,
    getUserInfo,
    clearUserInfo,
    registerUser
  }
}, {
  persist: true,
})
